﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NotificationControl.xaml.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Interaction logic for NotificationControl.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Control
{
    using System;
    using System.Windows;
    using System.Windows.Controls.Primitives;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using Hardcodet.Wpf.TaskbarNotification;

    using NLog;

    /// <summary>
    /// Interaction logic for NotificationControl
    /// </summary>
    public partial class NotificationControl
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Fields

        /// <summary>
        /// The is closing.
        /// </summary>
        private bool isClosing;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NotificationControl"/> class.
        /// </summary>
        public NotificationControl()
        {
            Logger.Trace("Init: NotificationControl");

            this.InitializeComponent();

            TaskbarIcon.AddBalloonClosingHandler(this, this.OnBalloonClosing);

            this.CloseButton.Visibility = Visibility.Hidden;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the close delay.
        /// </summary>
        public int CloseDelay { get; set; }

        /// <summary>
        /// Gets or sets the notification image.
        /// </summary>
        public string NotificationImage
        {
            get
            {
                BitmapImage image = (BitmapImage)this.NotificationImageElement.Source;

                return image.UriSource.ToString();
            }

            set
            {
                Uri uriSource = new Uri("/PresentationAid;component/Resources/" + value, UriKind.RelativeOrAbsolute);
                this.NotificationImageElement.Source = new BitmapImage(uriSource);
            }
        }

        /// <summary>
        /// Gets or sets the notification text.
        /// </summary>
        public string NotificationText
        {
            get
            {
                return this.NotificationTextTextBlock.Text;
            }

            set
            {
                this.NotificationTextTextBlock.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the notification title.
        /// </summary>
        public string NotificationTitle
        {
            get
            {
                return this.NotificationTitleTextBlock.Text;
            }

            set
            {
                this.NotificationTitleTextBlock.Text = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The close button click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CloseButtonClick(object sender, RoutedEventArgs e)
        {
            TaskbarIcon taskbarIcon = TaskbarIcon.GetParentTaskbarIcon(this);

            Logger.Trace("Event: CloseButtonClick - CloseBallon, TaskbarIcon: {0}", taskbarIcon);

            if (taskbarIcon != null)
            {
                taskbarIcon.CloseBalloon();
            }
        }

        /// <summary>
        /// The main border mouse enter.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void MainBorderMouseEnter(object sender, MouseEventArgs e)
        {
            if (this.isClosing)
            {
                return;
            }

            Logger.Trace("Event: MainBorderMouseEnter");

            this.MainBorder.BorderThickness = new Thickness(2);

            this.CloseButton.Visibility = Visibility.Visible;

            TaskbarIcon taskbarIcon = TaskbarIcon.GetParentTaskbarIcon(this);
            taskbarIcon.ResetBalloonCloseTimer();
        }

        /// <summary>
        /// The main border mouse leave.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void MainBorderMouseLeave(object sender, MouseEventArgs e)
        {
            Logger.Trace("Event: MainBorderMouseLeave");

            this.MainBorder.BorderThickness = new Thickness();

            this.CloseButton.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// The on balloon closing.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnBalloonClosing(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            this.isClosing = true;
        }

        /// <summary>
        /// Closes the popup once the fade-out animation completed.
        /// The animation was triggered in XAML through the attached
        /// BalloonClosing event.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnFadeOutCompleted(object sender, EventArgs e)
        {
            Popup pp = (Popup)this.Parent;
            pp.IsOpen = false;
        }

        #endregion
    }
}